package cn.fxl.servicemodule.service.impl;

import cn.fxl.basic.exception.BusinessExciption;
import cn.fxl.basic.service.IBaiduAiAuditService;
import cn.fxl.basic.service.impl.BasicServiceImpl;
import cn.fxl.basic.utils.LoginContext;
import cn.fxl.org.domain.Employee;
import cn.fxl.org.mapper.EmployeeMapper;
import cn.fxl.servicemodule.domain.Product;
import cn.fxl.servicemodule.domain.ProductDetail;
import cn.fxl.servicemodule.domain.ProductOnlineAuditLog;
import cn.fxl.servicemodule.mapper.ProductDetailMapper;
import cn.fxl.servicemodule.mapper.ProductMapper;
import cn.fxl.servicemodule.mapper.ProductOnlineAuditLogMapper;
import cn.fxl.servicemodule.service.IProductService;
import cn.fxl.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BasicServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IBaiduAiAuditService iBaiduAiAuditService;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void save(Product product) throws MessagingException {
        super.save(product);
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProductId(product.getId());
        productDetailMapper.save(productDetail);
    }

    @Override
    public void update(Product product) {
        super.update(product);
        if(product.getProductDetail()!=null){
            ProductDetail productDetail = product.getProductDetail();
            productDetail.setProductId(product.getId());
            productDetailMapper.update(productDetail);

        }
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
        productDetailMapper.remove(id);
    }

    @Override
    public void onsale(Long[] ids, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.getByLogininfoId(logininfo.getId());
        for (Long id : ids) {
            Product product = productMapper.getOne(id);
            Boolean aBoolean = iBaiduAiAuditService.textAudit(product.getName());
            Boolean aBoolean1 = iBaiduAiAuditService.imageAudit(product.getResources());
            if(aBoolean&&aBoolean1){
                product.setState(1L);
                product.setOnsaletime(new Date());
                productMapper.update(product);
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                productOnlineAuditLog.setProductId(product.getId());
                productOnlineAuditLog.setState(1);
                productOnlineAuditLog.setAuditId(employee.getId());
                productOnlineAuditLog.setNote("审核成功");
                productOnlineAuditLogMapper.save(productOnlineAuditLog);
            }else {
                product.setState(0L);
                productMapper.update(product);
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                productOnlineAuditLog.setNote("审核失败");
                productOnlineAuditLog.setProductId(product.getId());
                productOnlineAuditLog.setState(0);
                productOnlineAuditLog.setAuditId(employee.getId());
                productOnlineAuditLogMapper.save(productOnlineAuditLog);
                throw new BusinessExciption("审核失败:服务名称为【"+product.getName()+"】有违规信息");
            }
        }
    }

    @Override
    public void offsale(Long[] ids) {
        for (Long id : ids) {
            Product product = productMapper.getOne(id);
            if(product.getState()!=0){
                product.setState(0L);
                product.setOffsaletime(new Date());
                productMapper.update(product);
            }
        }
    }
}
